import config from '@/config'
import { getToken } from "@/utils/auth";
import { toast, showConfirm, tansParams } from '@/utils/common'
export function uploadFile(url) {
    return new Promise((resolve, reject) => {
        let a = uni.uploadFile({
            url: config.baseUrl + "/app/upload/file",
            filePath: url,
            name: "file",
            header: {
                accessToken: getToken(),
            },
            formData: {
            },
            success: (res) => {
                let result = JSON.parse(res.data)
                const code = result.code || 2003
                const msg = result.message || '操作成功'
                if (code === 200) {
                    let { result } = JSON.parse(res.data)
                    resolve(result);
                } else if (code == 401) {
                    showConfirm("登录状态已过期，您可以继续留在该页面，或者重新登录?").complete(res => {
                        store.dispatch('user/LogOut').then(res => {
                            uni.reLaunch({ url: '/pages/login/login' })
                        })
                    })
                    reject('无效的会话，或者会话已过期，请重新登录。')
                } else if (code === 500) {
                    toast(msg)
                    reject('500')
                } else if (code !== 200) {
                    toast(msg)
                    reject(code)
                }
            },
            fail: (error) => {
                let { errMsg } = error
                if (errMsg == 'Network Error') {
                    errMsg = '后端接口连接异常'
                } else if (errMsg.includes('timeout')) {
                    errMsg = '系统接口请求超时'
                } else if (errMsg.includes('Request failed with status code')) {
                    errMsg = '系统接口' + errMsg.substr(errMsg.length - 3) + '异常'
                }
                toast(errMsg)
                reject(error)
            }
        })
    });
}